package com.leecode.xiehf.page_11;

import com.leecode.Printer;

import java.util.HashMap;
import java.util.Map;

public class Solution_0523 extends Printer {

    public static void main(String[] args) {
        Solution_0523 solution = new Solution_0523();
        System.out.println(solution.checkSubarraySum(new int[]{23, 2, 4, 6, 7}, 6));
    }

    public boolean checkSubarraySum(int[] nums, int k) {
        int len = nums.length;
        if (len < 2) {
            return false;
        }
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(0, -1);
        int remainder = 0;
        for (int i = 0; i < len; i++) {
            remainder = (remainder + nums[i]) % k;
            if (map.containsKey(remainder)) {
                int prevIndex = map.get(remainder);
                if (i - prevIndex >= 2) {
                    return true;
                }
            } else {
                map.put(remainder, i);
            }
        }
        return false;
    }

}
